﻿using Himp.Platform;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace Himp.ShareData
{
    /// <summary>
    /// 患者基本信息
    /// </summary>
    public class PatnBaseInfoService : PlatformAppService, IPatnBaseInfoService
    {
        /// <summary>
        /// 
        /// </summary>
        private IRepository<PatnBaseInfo, Guid> _patnBaseInfoRepository;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="projectConfigRepository"></param>
        public PatnBaseInfoService(IRepository<PatnBaseInfo, Guid> patnBaseInfoRepository)
        {
            _patnBaseInfoRepository = patnBaseInfoRepository;
        }

        /// <summary>
        /// 获取患者基本信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<PatnBaseInfoDto>> GetListAsync(PatnBaseInfoInput input)
        {
            var iQuery = await _patnBaseInfoRepository.GetQueryableAsync();
            //iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.IptOtpNo), i => i.IptOtpNo == input.IptOtpNo);

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<PatnBaseInfo>, List<PatnBaseInfoDto>>(iQuery.ToList());

                return new PagedResultDto<PatnBaseInfoDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<PatnBaseInfo>, List<PatnBaseInfoDto>>(items.ToList());

                return new PagedResultDto<PatnBaseInfoDto>(totalCount, itemsDto);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PatnBaseInfoDto> PostAsync(PatnBaseInfoCreateDto input)
        {
            var patnBaseInfo = ObjectMapper.Map<PatnBaseInfoCreateDto, PatnBaseInfo>(input);
            patnBaseInfo = await _patnBaseInfoRepository.InsertAsync(patnBaseInfo);
            var itemDto = ObjectMapper.Map<PatnBaseInfo, PatnBaseInfoDto>(patnBaseInfo);

            return itemDto;
        }
    }
}
